package com.qf.es.index;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.es.pojo.SmsIndex;
import com.qf.es.utils.ESClient;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DocService {

    private String index="tcy-logs-index";
    private ObjectMapper objectMapper=new ObjectMapper();


    public void addDoc(String id) throws Exception {
        RestHighLevelClient client = ESClient.getClient();
        SmsIndex smsIndex=new SmsIndex();
        smsIndex.setCorpName("qianfeng");
        smsIndex.setFee(11);
        smsIndex.setCreateDate(new Date());
        smsIndex.setMobile("1336666666");
        smsIndex.setLongCode("123456");
        smsIndex.setProvince("铁岭；scv");
        smsIndex.setState(1);
        smsIndex.setSendDate(new Date());
        String json = objectMapper.writeValueAsString(smsIndex);
        //创建请求,并指定 json 数据
        IndexRequest request=new IndexRequest(index).source(json, XContentType.JSON);
        if(id!=null){
            request.id(id);
        }
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);
        System.err.println(response);
    }

    public void get(String id) throws IOException {
        RestHighLevelClient client = ESClient.getClient();
        GetRequest getRequest = new GetRequest(index).id(id);//zwv5ynwBJbTDI88bqNUd
        GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
        System.err.println(response);
    }

    public void delete(String id) throws IOException {
        RestHighLevelClient client = ESClient.getClient();
        DeleteRequest deleteRequest=new DeleteRequest(index,id);
        DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);
        System.err.println(delete);
    }

    public void update(String id) throws Exception {
        RestHighLevelClient client = ESClient.getClient();
        Map map=new HashMap<>();
        map.put("province", "昌平区");
        String json = objectMapper.writeValueAsString(map);
        UpdateRequest request = new UpdateRequest(index, id).doc(json, XContentType.JSON);
        UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
    }

    public void termQuery(String field,String value) throws IOException {
        baseQuery(QueryBuilders.termQuery(field, value));

    }

    public void termsQuery(String field,String... value) throws IOException {
        baseQuery(QueryBuilders.termQuery(field,value));
    }

    public void matchQuery(String field,String value) throws IOException {
        baseQuery(QueryBuilders.matchQuery(field,value));
    }

    public void matchAll() throws IOException {
        baseQuery(QueryBuilders.matchAllQuery());
    }

    public void matchBool(String field,String value) throws IOException {
        baseQuery(QueryBuilders.matchQuery(field,value).operator(Operator.AND));
    }

    public void multi_match(String value,String... field) throws IOException {
        baseQuery(QueryBuilders.multiMatchQuery(value,field));
    }

    public void idsQuery(String...ids) throws IOException {
        baseQuery(QueryBuilders.idsQuery().addIds(ids));
    }

    public void prefixQuery(String field,String value) throws IOException {
        baseQuery(QueryBuilders.prefixQuery(field,value));
    }

    public void fuzzyQuery(String field,String value) throws IOException {
        baseQuery(QueryBuilders.fuzzyQuery(field,value).prefixLength(2));
    }

    public void wildcardQuery(String field,String value) throws IOException {
        baseQuery(QueryBuilders.wildcardQuery(field,value));
    }

    public void rangeQuery(String field,String from,String to) throws IOException {
        baseQuery(QueryBuilders.rangeQuery(field).gt(from).lt(to));
    }

    public void boolQuery() throws IOException {

        BoolQueryBuilder builder = QueryBuilders.boolQuery().should(QueryBuilders.termQuery("province", "武汉")).should(QueryBuilders.termQuery("province", "北京"))
                .must(QueryBuilders.matchQuery("smsContent", "中国")).must(QueryBuilders.matchQuery("smsContent", "平安"))
                .mustNot(QueryBuilders.termQuery("operatorId", 2));
        baseQuery(builder);
    }

    public void boostQuery() throws IOException {

        BoostingQueryBuilder queryBuilder = QueryBuilders.boostingQuery(QueryBuilders.matchQuery("smsContent", "收货安装"), QueryBuilders.matchQuery("smsContent", "王五"))
                .negativeBoost(0.1f);
        baseQuery(queryBuilder);
    }

    public void deleteByQuery(String field,String from,String to) throws IOException {
        RestHighLevelClient client = ESClient.getClient();

        DeleteByQueryRequest request=new DeleteByQueryRequest().setQuery(QueryBuilders.rangeQuery(field).gt(from).lt(to));
        client.deleteByQuery(request,RequestOptions.DEFAULT);

    }

    private SearchResponse baseQuery(QueryBuilder queryBuilder) throws IOException {
        RestHighLevelClient client = ESClient.getClient();
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//创建封装查询的 builder
        searchSourceBuilder.query(queryBuilder);//给 builder 设置查询的方式
        searchRequest.source(searchSourceBuilder);//给搜索的请求设置查询条件

        SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
        System.err.println(response);
        return response;
    }
}
